package com.teaching.teachingsupport.service.impl.teacherserviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teaching.teachingsupport.common.pojo.dataobject.*;
import com.teaching.teachingsupport.common.pojo.dto.response.TeachingScheduleDTO;
import com.teaching.teachingsupport.common.utils.SecurityUtils;
import com.teaching.teachingsupport.mapper.TeacherMapper;
import com.teaching.teachingsupport.mapper.studentviewmapper.CourseMapper;
import com.teaching.teachingsupport.mapper.studentviewmapper.SemesterMapper;
import com.teaching.teachingsupport.mapper.teacherviewmapper.TeacherCourseMapper;
import com.teaching.teachingsupport.service.service2.teacherservice.TeachingScheduleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class TeachingScheduleServiceImpl implements TeachingScheduleService {

    private final SecurityUtils securityUtils;
    private final TeacherMapper teacherMapper;
    private final TeacherCourseMapper teacherCourseMapper;
    private final CourseMapper courseMapper;
    private final SemesterMapper semesterMapper;

    @Override
    public Page<TeachingScheduleDTO> getTeachingSchedule(String username, String password, Integer page) {
        // 1. 验证用户
        User user = securityUtils.validateUser(username, password);
        if (user == null) {
            return new Page<>();
        }

        return getTeachingScheduleByUserId(user.getUserId(), page);
    }

    @Override
    public Page<TeachingScheduleDTO> getTeachingScheduleByUserId(Long userId, Integer page) {
        // 1. 获取教师信息
        Teacher teacher = teacherMapper.selectOne(
                new LambdaQueryWrapper<Teacher>()
                        .eq(Teacher::getUserId, userId)
        );
        if (teacher == null) {
            return new Page<>();
        }

        // 2. 分页查询教师课程
        Page<TeacherCourse> coursePage = teacherCourseMapper.selectPage(
                new Page<>(page, 10),
                new LambdaQueryWrapper<TeacherCourse>()
                        .eq(TeacherCourse::getTeacherId, teacher.getUserId())
                        .orderByDesc(TeacherCourse::getSemesterId)
        );

        // 3. 转换为VO
        Page<TeachingScheduleDTO> resultPage = new Page<>();
        // 设置分页信息
        resultPage.setCurrent(coursePage.getCurrent());
        resultPage.setSize(coursePage.getSize());
        resultPage.setTotal(coursePage.getTotal());
        resultPage.setPages(coursePage.getPages());

        resultPage.setRecords(
                coursePage.getRecords().stream().map(tc -> {
                    TeachingScheduleDTO vo = new TeachingScheduleDTO();
                    // 获取课程信息
                    Course course = courseMapper.selectById(tc.getCourseId());
                    if (course != null) {
                        vo.setCourseName(course.getCourseName());
                        vo.setCourseCode(course.getCourseCode());
                        vo.setCredit(course.getCredit());
                    }
                    // 获取学期信息
                    Semester semester = semesterMapper.selectById(tc.getSemesterId());
                    if (semester != null) {
                        vo.setSemesterName(semester.getTermName());
                    }
                    // 设置其他信息
                    vo.setClassLocation(tc.getClassLocation());
                    vo.setClassTime(tc.getClassTime());
                    vo.setStatus(tc.getStatus());
                    vo.setMaxStudent(tc.getMaxStudent());
                    return vo;
                }).collect(Collectors.toList())
        );

        return resultPage;
    }
}
